package com.health.mapp.impl;

import java.util.Arrays;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.health.db.model.GoodsList;
import com.health.db.model.GoodsListTemp;
import com.health.db.model.GoodsOrder;
import com.health.db.model.GoodsOrderTemp;
import com.health.db.service.sm.IGoodsOrderService;
import com.health.mapp.bean.GenerateGoodsOrderByCSReq;
import com.health.mapp.bean.GenerateGoodsOrderByCSResp;
import com.health.mapp.bean.Header;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.PropertiesUtil;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 客服下单
 * @author YuChong
 *
 */
public class GenerateGoodsOrderByCSImpl implements ActionInterface {
	
	@Autowired
	private IGoodsOrderService goodsOrderService;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
	 
		GenerateGoodsOrderByCSReq req = (GenerateGoodsOrderByCSReq) reqBean;
		GenerateGoodsOrderByCSResp _resp = new GenerateGoodsOrderByCSResp();
		
		String goodsId = req.getGoodsId();
		String userId = req.getUserId();
		String businessCode = req.getBusinessCode();
		
		 String custemUserIds =PropertiesUtil.getString("miniprogram.custemUserId");
	        
        if (StringUtils.isNotEmpty(custemUserIds)) {
        	String[] custemUserId = custemUserIds.split(",");
        	
         Boolean isCustom=Arrays.asList(custemUserId).contains(userId);
	         if (!isCustom) {
	        		_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_8885);
					_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_8885_NOTE);
			}
		}
		
	
		if (StringUtils.isNotBlank(goodsId)&&StringUtils.isNotBlank(userId)) {
			//type =0 新增  1 修改   
			
			GoodsOrder goodsOrder = new GoodsOrder();
			//goodsOrder.setOwnerid(Long.valueOf(userId));
			goodsOrder.setGoodsid(Long.valueOf(goodsId));
			//goodsOrder.setOwenerName(req.getUserName());
			//已支付
			goodsOrder.setRunstatusid(Constant.CONSTANTS_ORDER_RUNSTATUS_ID_5);
			goodsOrder.setRunStatus(Constant.CONSTANTS_ORDER_RUNSTATUS_DES_5);
			goodsOrder.setPayFlag(String.valueOf(Constant.CONSTANTS_IS_PAYMENT_FALSE));
			goodsOrder.setPayTypeId(Constant.CONSTANTS_IS_PAYMENT_FALSE);
			
			if (StringUtils.isNotBlank(businessCode)) {
				goodsOrder.setBusinessCode(businessCode);
			}
			goodsOrder =goodsOrderService.insert(goodsOrder);
			
			if (null==goodsOrder.getRecid()) {
				 
				_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3001);
				_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3001_NOTE);
			}else{
				
				GoodsOrderTemp temp = new GoodsOrderTemp();
				temp = this.copyProperties(goodsOrder, temp);
				
				_resp.setGoodsOrder(temp);
			}
		}else{
			
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ERROR);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ERROR_NOTE);
			return _resp;
		}
		
		return _resp;
	}
	
	/**
	 * 参数全部转换成string回传
	 * @param goodsOrder
	 * @param temp
	 * @return
	 */
	private GoodsOrderTemp copyProperties(GoodsOrder goodsOrder,GoodsOrderTemp temp){
		
		String now = DateUtils.currentDatetime();
		//recid
		temp.setOrderId(goodsOrder.getRecid().toString());
	    temp.setTotal(goodsOrder.getTotal().toString());
	    temp.setPayTypeId(goodsOrder.getPayTypeId().toString());
	    temp.setRefBillId(null==goodsOrder.getRefBillid()?"":goodsOrder.getRefBillid().toString());
	    temp.setGoodsType(goodsOrder.getGoodsType());
	    temp.setCreateDate(now);
	    temp.setRunstatusId(goodsOrder.getRunstatusid().toString());
	    temp.setRunStatus(goodsOrder.getRunStatus());
	    temp.setUpdateDate(now);
	    temp.setRunStatus(goodsOrder.getRunStatus());
	    temp.setPayFlag(goodsOrder.getPayFlag());
	    temp.setGiftAmount(goodsOrder.getGiftAmount().toString());
	    GoodsListTemp simple = new GoodsListTemp();
	    
	    GoodsList goodsList = goodsOrder.getGoodsList();
	    
	    simple.setGoodsId(goodsList.getGoodsid().toString());
	    simple.setGoodsName(goodsList.getGoodsName());
	    simple.setGoodsMeasure(goodsList.getGoodsMeasure());
	    simple.setSaleListPrice(goodsList.getSaleListPrice().toString());
	    simple.setSalePrice(goodsList.getSalePrice().toString());
	    simple.setCostPrice(goodsList.getCostPrice().toString());
	    simple.setTypeId(goodsList.getTypeid().toString());
	    simple.setTypeName(goodsList.getTypeName());
	    simple.setGoodsSpec(goodsList.getGoodsSpec());
	    simple.setGoodsDesc(goodsList.getGoodsDesc());
	    //simple.setSort(goodsList.getBrandid().toString());
	    temp.setGoodsList(simple);
	    
		return temp;
	}
	
	public IGoodsOrderService getGoodsOrderService() {
		return goodsOrderService;
	}
	public void setGoodsOrderService(IGoodsOrderService goodsOrderService) {
		this.goodsOrderService = goodsOrderService;
	}
	
}
